package com.starlight.flower_server.infrastructure.repository.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.starlight.flower_server.infrastructure.entity.FlowerUserRole;
import com.starlight.flower_server.infrastructure.mapper.FlowerUserRoleMapper;
import com.starlight.flower_server.infrastructure.repository.FlowerUserRoleRepository;
import com.starlight.flower_server.infrastructure.util.CurrentUserInfoUtils;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Repository;

import java.util.Date;

/**
 * @Author yfy2001
 * @date 2023/4/18 14:08
 */
@Repository
public class FlowerUserRoleRepositoryImpl implements FlowerUserRoleRepository {

    @Resource
    private FlowerUserRoleMapper flowerUserRoleMapper;

    @Override
    public FlowerUserRole queryDetail(Long id) {
        return flowerUserRoleMapper.selectById(id);
    }

    @Override
    public FlowerUserRole queryOneByUserId(Long userId) {
        return flowerUserRoleMapper.selectOne(new LambdaQueryWrapper<FlowerUserRole>()
                .eq(FlowerUserRole::getFlowerUserId,userId));
    }

    @Override
    public Integer insert(FlowerUserRole flowerUserRole) {
        flowerUserRole.setCreator("SYSTEM");
        flowerUserRole.setModifier("SYSTEM");
        flowerUserRole.setCreated(new Date());
        flowerUserRole.setModified(new Date());
        flowerUserRole.setIsDeleted(0);
        return flowerUserRoleMapper.insert(flowerUserRole);
    }

    @Override
    public Integer modify(FlowerUserRole flowerUserRole) {
        String modifier= CurrentUserInfoUtils.get("username");
        flowerUserRole.setModifier(modifier);
        flowerUserRole.setModified(new Date());
        return flowerUserRoleMapper.updateById(flowerUserRole);
    }

    @Override
    public Integer logicDelete(Long id) {
        return flowerUserRoleMapper.deleteById(id);
    }
}
